@echo off
echo 🚀 IoT Cloud Platform Setup
echo ================================

echo 📋 Checking prerequisites...

REM Check Go
go version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Go is not installed. Please install Go 1.21+
    exit /b 1
)
echo ✅ Go is available

REM Check Docker
docker --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Docker is not installed. Please install Docker
    exit /b 1
)
echo ✅ Docker is available

REM Check Docker Compose
docker-compose --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Docker Compose is not installed. Please install Docker Compose
    exit /b 1
)
echo ✅ Docker Compose is available

echo.
echo 🔧 Setting up the environment...

REM Create necessary directories
if not exist "logs" mkdir logs
if not exist "data" mkdir data
if not exist "data\postgres" mkdir data\postgres
if not exist "data\redis" mkdir data\redis
if not exist "data\influxdb" mkdir data\influxdb
if not exist "data\kafka" mkdir data\kafka

REM Download dependencies
echo 📦 Installing Go dependencies...
go mod tidy

REM Copy configuration file if it doesn't exist
if not exist "config\config.local.yaml" (
    echo 📝 Creating local configuration file...
    copy "config\config.yaml" "config\config.local.yaml"
    echo ✅ Configuration file created at config\config.local.yaml
    echo 📌 Please edit this file with your specific settings
)

echo.
echo 🐳 Starting infrastructure services with Docker Compose...

REM Start infrastructure services
docker-compose up -d postgres redis influxdb kafka zookeeper emqx

echo ⏳ Waiting for services to be ready...
timeout /t 30 /nobreak

echo 🔍 Checking service health...
docker-compose ps

echo.
echo 🏗️  Building the application...

REM Create bin directory
if not exist "bin" mkdir bin

REM Build the application
go build -o bin\iot-platform.exe cmd\main.go

echo ✅ Application built successfully

echo.
echo 🎉 Setup completed successfully!
echo.
echo 📚 Next steps:
echo 1. Edit config\config.local.yaml with your settings
echo 2. Start the application: bin\iot-platform.exe
echo 3. Or run in development mode: go run cmd\main.go
echo 4. Visit http://localhost:8080/health to verify
echo.
echo 📖 Documentation:
echo - API Documentation: http://localhost:8080/api/v1/docs
echo - EMQX Dashboard: http://localhost:18083 (admin/public)
echo - Grafana Dashboard: http://localhost:3000 (admin/admin)
echo - Prometheus: http://localhost:9090
echo.
echo 🔐 Default credentials:
echo - PostgreSQL: postgres/password
echo - EMQX: admin/public
echo - Grafana: admin/admin
echo.
echo ⚠️  Security notice: Please change default passwords in production!
echo.
echo Happy coding! 🚀

pause